package com.lanp.native_test.ExecutorsRun.CustomTheadExecutor;

import org.apache.commons.lang3.RandomStringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;

public class LinkedBlockingQueue_Test {
    /*
    链表、无边界阻塞队列、先进先出
    最大任务容量是 最大线程数 + 队列最大长度
    不超过 阻塞队列数量，执行核心线程数量
    超过 阻塞队列数量，执行最大线程数量
     */
    static BlockingQueue blockingQueue = new LinkedBlockingQueue(15);


    private static final ThreadPoolExecutor TEST_EXECUTORS =
            new ThreadPoolExecutor(1, 4, 10, TimeUnit.MINUTES
                    , blockingQueue, Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 20; i++) {
            final int fi = i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    Date nowDate = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    System.out.println(sdf.format(nowDate) + "   " + fi + "-索引-newFixedThreadPool:" + RandomStringUtils.randomAlphabetic(10));
                }
            };
            System.out.println("准备添加到队列");
            TEST_EXECUTORS.execute(runnable);
        }

    }
}
